package com.hs.Compiler;

import org.junit.Test;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Author: hs
 * @Description:
 * @Date: Create in 11:31 2019/9/4
 */
public class Compiler {
    @Test
    public void compiler1(){
        String javaAbsolutePath = "C:\\Users\\Administrator\\Desktop\\java\\A.java";
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null, null, null, "-encoding", "UTF-8", "-classpath", javaAbsolutePath.toString(), javaAbsolutePath);
    }
    @Test
    public void compiler2(){
        String javaAbsolutePath = "C:\\Users\\Administrator\\Desktop\\java\\A.java";

        try {
            Process process = Runtime.getRuntime().exec("javac -classpath C:\\Users\\Administrator\\Desktop\\java " + javaAbsolutePath);
            InputStream errorStream = process.getErrorStream();
            //InputStream inputStream = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            while ((line=bufferedReader.readLine()) != null){
                System.out.println(line);
            }
            int exitVal = process.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
